<template>
  <div>
    <!-- 零售单审核（ARCFOX） -->
    <TopFilter
      :list="topFilterList"
      :cols="3"
      @filterChange="changeHandle"
      @onCollapse="collapseHandle"
      :labelWidth="110"
    ></TopFilter>
    <FilterTable
      ref="table"
      :columns="columns"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :datakey="'records'"
      :onRowSelectChange="selectChangeHandle"
      :onColumnsChange="columns => (this.columns = columns)"
    >
      <template slot="controls">
        <multiuse-button
          size="small"
          type="primary"
          :disabled="!isCan"
          :click="commitHandle.bind(this, 60341004)"
        >审批通过</multiuse-button>
        <multiuse-button
          size="small"
          type="primary"
          :disabled="!isCan"
          :click="commitHandle.bind(this, 60341005)"
        >审批驳回</multiuse-button>
      </template>
    </FilterTable>

    <Drawer
      :visible.sync="visible"
      :title="drawerTitle"
      destroyOnClose
      :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }"
    >
      <SonModel :type="handleType" :rowData="formData" @close="emitCloseHandler" />
    </Drawer>
    <BaseDialog
      :visible.sync="isReject"
      :title="'驳回意见'"
      destroyOnClose
      :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }"
    >
      <Reject :rowData="rejectData" @close="emitCloseHandler1" />
    </BaseDialog>
  </div>
</template>
<script>
import { dictionary } from '@/utils/dictMixin';
import { queryList, auditRetailOrder } from '@/api/dealerSale/SalesSanagement/RetailOrderReviewAF';
import SonModel from './SonModel';
import Reject from './Reject';

export default {
  name: 'RetailOrderReview',
  mixins: [dictionary],

  data() {
    this.BaseTable = null;
    this.conf = { add: '新增', edit: '编辑', show: '明细' };
    return {
      topFilterList: this.createdFrom(),
      columns: this.createdTable(),
      fetchApi: queryList,
      fetchParams: { xhrAbort: true },
      formData: {},
      rejectData: {},
      saveData: [],
      visible: false,
      isReject: false,
      isCan: false,
      handleType: '',
      drawerTitle: ''
    };
  },
  components: {
    SonModel,
    Reject
  },
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
  },
  created() {},
  methods: {
    // 搜索
    changeHandle(params) {
      this.fetchParams = Object.assign({}, this.fetchParams, params, { xhrAbort: false });
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    async saveOrCommitHandle(data) {
      let saveData = [];
      data.map(x => {
        let json = {
          orderId: x.orderId,
          checkDesc: x.checkDesc,
          orderStatus: 60341004
        };
        saveData.push(json);
      });

      const res = await auditRetailOrder(saveData);
      if (res.resultCode == 200) {
        // 重新加载
        this.fetchParams = { ...this.fetchParams };
      }
    },
    async commitHandle(type) {
      if (type == 60341004) {
        await this.$confirm('是否通过?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(async () => {
            await this.saveOrCommitHandle(this.saveData);
          })
          .catch(() => {});
      }
      if (type == 60341005) {
        this.isReject = true;
        this.rejectData = this.saveData;
      }
    },
    // 编辑
    editHandle(row) {
      this.handleType = 'edit';
      this.formData = row;
      this.showHandler();
    },
    // 明细
    detailHandle(row) {
      this.handleType = 'show';
      this.formData = row;
      this.showHandler();
    },
    showHandler() {
      this.drawerTitle = `${this.conf[this.handleType]}`;
      this.visible = true;
    },
    emitCloseHandler(val, type) {
      this.visible = val;
      if (type === 'refresh') {
        // 重新加载
        this.fetchParams = { ...this.fetchParams };
      }
    },
    emitCloseHandler1(val, type) {
      this.isReject = val;
      if (type === 'refresh') {
        // 重新加载
        this.fetchParams = { ...this.fetchParams };
      }
    },
    selectChangeHandle(rows) {
      if (rows.length > 0) {
        this.isCan = true;
        this.saveData = rows;
        return;
      }
      this.isCan = false;
    },

    createdFrom() {
      return [
        {
          type: 'TEXT_AREA',
          label: 'VIN',
          fieldName: 'vin',
          placeholder: '请输入',
          maxlength: '',
          maxRows: 2
        },
        {
          type: 'INPUT',
          label: '零售单编号',
          fieldName: 'orderNo',
          placeholder: '请输入'
        },
        {
          type: 'RANGE_DATE',
          label: '创建日期',
          fieldName: 'createStart|createEnd',
          placeholder: '请选择',
          initialValue: []
        },
        {
          type: 'INPUT',
          label: '客户名称',
          fieldName: 'customerName',
          placeholder: '请输入'
        },
        {
          type: 'INPUT',
          label: '手机号',
          fieldName: 'customerPhone',
          placeholder: '请输入'
        },
        {
          type: 'INPUT',
          label: '产品名称',
          fieldName: 'productName',
          placeholder: '请输入'
        },
        // {
        //   type: 'SELECT',
        //   label: '订单类型',
        //   fieldName: 'orderType',
        //   placeholder: '请选择',
        //   itemList: this.createDictList('6026')
        // },
        {
          type: 'SELECT',
          label: '订单状态',
          fieldName: 'orderStatus',
          placeholder: '请选择',
          itemList: [{ text: '审核中', value: 60341003 }]
        }
      ];
    },
    createdTable() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          fixed: 'left',
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          fixed: 'left',
          render: (props, h) => {
            return (
              <div>
                <multiuse-button size="mini" type="text" onClick={() => this.editHandle(props.row)}>
                  编辑
                </multiuse-button>
                <multiuse-button size="mini" type="text" onClick={() => this.detailHandle(props.row)}>
                  明细
                </multiuse-button>
              </div>
            );
          }
        },

        {
          title: '订单状态',
          dataIndex: 'orderStatus',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: [{ text: '审核中', value: 60341003 }],
          render: props => {
            return <span>{this.createDictText(props.row.orderStatus, '6034')}</span>;
          }
        },
        {
          title: '零售单号',
          dataIndex: 'orderNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '客户编号',
          dataIndex: 'customerNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '客户名称',
          dataIndex: 'customerName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },

        // {
        //   title: '大客户订单',
        //   dataIndex: 'isMajor',
        //   sorter: true,
        //   filter: true,
        //   filterType: 'checkbox',
        //   filterItems: this.createDictList('1004'),
        //   render: props => {
        //     return <span>{this.createDictText(props.row.isMajor, '1004')}</span>;
        //   }
        // },
        // {
        //   title: '特殊订单',
        //   dataIndex: 'isSpecial',
        //   sorter: true,
        //   filter: true,
        //   filterType: 'checkbox',
        //   filterItems: this.createDictList('1004'),
        //   render: props => {
        //     return <span>{this.createDictText(props.row.isSpecial, '1004')}</span>;
        //   }
        // },

        // {
        //   title: '订单来源',
        //   dataIndex: 'orderSource',
        //   sorter: true,
        //   filter: true,
        //   filterType: 'checkbox',
        //   filterItems: this.createDictList('6042'),
        //   render: props => {
        //     return <span>{this.createDictText(props.row.orderSource, '6042')}</span>;
        //   }
        // },
        {
          title: '订单类型',
          dataIndex: 'orderType',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('6026'),
          render: props => {
            return <span>{this.createDictText(props.row.orderType, '6026')}</span>;
          }
        },
        {
          title: '创建时间',
          dataIndex: 'createdAt',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '配车时间',
          dataIndex: 'distributionDate',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '手机号',
          dataIndex: 'customerPhone',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '销售顾问',
          dataIndex: 'salesManName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '销售指导价',
          dataIndex: 'basicPrice',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '销售报价',
          dataIndex: 'salesQuatation',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '支付方式',
          dataIndex: 'payType',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('6015'),
          render: props => {
            return <span>{this.createDictText(props.row.payType, '6015')}</span>;
          }
        },
        // {
        //   title: '定金金额',
        //   dataIndex: 'depositAmount',
        //   sorter: true,
        //   filter: true,
        //   filterType: 'input'
        // },
        // {
        //   title: '定金日期',
        //   dataIndex: 'depositDate',
        //   sorter: true,
        //   filter: true,
        //   filterType: 'input'
        // },
        {
          title: 'VIN',
          dataIndex: 'vin',
          align: 'center',
          width: 180,
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '产品代码',
          dataIndex: 'productCode',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '产品名称',
          dataIndex: 'productName',
          align: 'center',
          width: 400,
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '车系名称',
          dataIndex: 'seriesName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '车型名称',
          dataIndex: 'modelName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '配置名称',
          dataIndex: 'packageName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '选装名称',
          dataIndex: 'optionName',
          align: 'center',
          width: 250,
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '内饰',
          dataIndex: 'trimColorName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '外饰',
          dataIndex: 'colorName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '备注',
          dataIndex: 'remark',
          sorter: true,
          filter: true,
          filterType: 'input'
        }
      ];
    }
  }
};
</script>
